#version 320 es
precision mediump float;
out vec4 color;
in vec2 u_v;
in float h;
uniform sampler2D tex0;
struct TerrainTexture{
    sampler2D materail;       //纹理材质
    vec4 region;            //管辖区域,w=1.0f/region.z - region.y
};
uniform TerrainTexture loess;
uniform TerrainTexture grassland;
uniform TerrainTexture rock;
uniform TerrainTexture snow;
/**
 * 计算可见度
 * @param h
 */
float scaleVisibility(vec4 region){
    if(h <= region.x || h >= region.z){
        return 0.0f;
    }
    if(h > region.y){
        //return (region.z - h) * 1.0f / (region.z - region.y);
        return (region.z - h) * region.w;
    }
    else{
        //return (h - region.x) * 1.0f / (region.z - region.y);
        return (h - region.x) * region.w;
    }
}
void main(void) {
    float f = scaleVisibility(loess.region);
    if(f > 0.0f){
        color += f * texture(loess.materail, u_v);
    }
    f = scaleVisibility(grassland.region);
    if(f > 0.0f){
        color += f * texture(grassland.materail, u_v);
    }
    f = scaleVisibility(rock.region);
    if(f > 0.0f){
        color += f * texture(rock.materail, u_v);
    }
    //防止最高处为黑色
    f = scaleVisibility(snow.region);
    if(f > 0.0f){
        color += f * texture(snow.materail, u_v);
    }
//    if(color.r == 0.0f && color.g == 0.0f && color.b == 0.0f)
//        color = vec4(1.0f);
    color = vec4(vec3(color), 1.0f);
    //color = texture(tex0, u_v);
}
